<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
             label-width="180px">
      <!--      <el-form-item label="创建时间" prop="createDate">-->
      <!--        <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="id" prop="id">-->
      <!--        <el-input v-model="dataForm.id" placeholder="id"></el-input>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="" prop="stationId">-->
      <!--        <el-input v-model="dataForm.stationId" placeholder=""></el-input>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="监测装置编码" prop="cmdId">-->
      <!--        <el-input v-model="dataForm.cmdId" placeholder="监测装置编码"></el-input>-->
      <!--      </el-form-item>-->
      <el-form-item label="倾斜度" prop="inclination">
        <el-input-number v-model="dataForm.inclination" :step="0.1" :precision="1"></el-input-number>
        <span class="unit_item">mm/m</span>
        <!--        <span class="supplementary_instruction">说明：</span>-->
      </el-form-item>
      <el-form-item label="顺线倾斜度" prop="inclinationX">
        <el-input-number v-model="dataForm.inclinationX" :step="0.1" :precision="1"></el-input-number>
        <span class="unit_item">mm/m</span>
        <!--        <span class="supplementary_instruction">说明：</span>-->
      </el-form-item>
      <el-form-item label="横向倾斜度" prop="inclinationY">
        <el-input-number v-model="dataForm.inclinationY" :step="0.1" :precision="1"></el-input-number>
        <span class="unit_item">mm/m</span>
        <!--        <span class="supplementary_instruction">说明：</span>-->
      </el-form-item>
      <el-form-item label="顺线倾斜角" prop="angleX">
        <el-input-number v-model="dataForm.angleX" :step="0.1" :precision="1"></el-input-number>
        <span class="unit_item">°</span>
        <!--        <span class="supplementary_instruction">说明：</span>-->
      </el-form-item>
      <el-form-item label="横向倾斜角" prop="angleY">
        <el-input-number v-model="dataForm.angleY" :step="0.1" :precision="1"></el-input-number>
        <span class="unit_item">°</span>
        <!--        <span class="supplementary_instruction">说明：</span>-->
      </el-form-item>
      <el-form-item label="金具温度一级预警" prop="temperatureH">
        <el-input-number v-model="dataForm.temperatureH" :step="0.1" :precision="1"></el-input-number>
        <span class="unit_item">℃</span>
        <!--        <span class="supplementary_instruction">说明：</span>-->


        <el-switch v-model="dataForm.delivery"></el-switch>

      </el-form-item>

      <el-form-item label="金具温度二级预警" prop="temperatureL">
        <el-input-number v-model="dataForm.temperatureL" :step="0.1" :precision="1"></el-input-number>
        <span class="unit_item">℃</span>
        <!--        <span class="supplementary_instruction">说明：</span>-->
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {reactive, ref} from "vue";
import baseService from "@/service/baseService";
import {useI18n} from "vue-i18n";
import {ElMessage} from "element-plus";

const {t} = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();

const dataForm = reactive({
  createDate: "",
  id: "",
  stationId: "",
  cmdId: "",
  inclination: "",
  inclinationX: "",
  inclinationY: "",
  angleX: "",
  angleY: "",
  temperatureH: "",
  temperatureL: "",
  isTemperatureL: "",

});

const rules = ref({});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/KxSensorWarnValue/kxsensorwarnvalue/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    if (dataForm.temperatureH < dataForm.temperatureL) {
      ElMessage.warning("设备告警!")
    }
    (!dataForm.id ? baseService.post : baseService.put)("/KxSensorWarnValue/kxsensorwarnvalue", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>

<style lang="scss" scoped>
.el-input__suffix-inner {
  color: #17b4d7;
}

.unit_item {
  display: inline-block;
  width: 60px;
  color: #17b4d7;
  font-size: 18px;
  margin-left: 10px;
}

.supplementary_instruction {
  color: #FFFFFF;
}
</style>
